package com.mg.admin.service.admin.impl;

import com.mg.admin.bean.admin.WxFansVO;
import com.mg.admin.dao.admin.WxFansMapper;
import com.mg.admin.page.PageBean;
import com.mg.admin.page.PageParam;
import com.mg.admin.service.admin.WxFansService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WxFansServiceImpl implements WxFansService {

	@Autowired
	private WxFansMapper wxFansMapper;

	@Override
	public PageBean queryForPage(WxFansVO wxFansVO, PageParam pageParam) {
		RowBounds rb = new RowBounds(pageParam.getStart(),pageParam.getNumPerPage());
		List<WxFansVO> list = wxFansMapper.queryForPage(wxFansVO,rb);
		int totalCount = wxFansMapper.queryCountForPage(wxFansVO);
		PageBean pageBean = new PageBean(pageParam.getCurrentPage(),pageParam.getNumPerPage(),totalCount,list);
		return pageBean;
	}

	@Override
	@Transactional
	public void saveSubscribeUser(WxFansVO wxFansVO) {
		List<WxFansVO> list = wxFansMapper.findFansByOpenId(wxFansVO);
		if (list.size() >0){
			wxFansMapper.updateSubscribeUser(wxFansVO);
		}else {
			wxFansMapper.insertSubscribeUser(wxFansVO);
		}
	}

	@Override
	public PageBean distPageList(WxFansVO wxFansVO, PageParam pageParam) {
		RowBounds rb = new RowBounds(pageParam.getStart(),pageParam.getNumPerPage());
		List<WxFansVO> list = wxFansMapper.distPageList(wxFansVO,rb);
		int totalCount = wxFansMapper.distPageCount(wxFansVO);
		PageBean pageBean = new PageBean(pageParam.getCurrentPage(),pageParam.getNumPerPage(),totalCount,list);
		return pageBean;
	}


}
